<template>
  <div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
      error-text="请求失败，点击重新加载">
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
// 获取搜索结果请求
import { getSearchResults } from '@/api/search.js'
export default {
  data () {
    return {
      // 列表内容
      list: [],
      // 是否显示加载状态
      loading: false,
      // 全部加载是否完成
      finished: false,
      page: 1,
      per_page: 10,
      error: false
    }
  },
  props: {
    // 父传子输入框内容
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      // 获取搜索结果请求
      try {
        const { data: res } = await getSearchResults({
          page: this.page,
          per_page: this.per_page,
          q: this.value
        })
        // 随机错误
        if (Math.random() > 0.5) {
          console.lg(123)
        }
        this.list.push(...res.data.results)
        this.loading = false
        if (this.list.length === res.data.total_count) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
